## Warning: package 'rmarkdown' was built under R version 3.5.2

 

Date: 2019-07-10
R version: 3.5.0
*Corresponding author: matthew.malishev@gmail.com
This document can be found at https://github.com/darwinanddavis/UsefulCode

Overview

Same deal as Useful Code, but the second instalment because the first one has too much stuff in it and now runs slow.

D3

devtools::install_github("jcheng5/d3scatter")
library(d3scatter)
library(crosstalk)
library(leaflet)
library(tibble)

sd <- SharedData$new(quakes[sample(nrow(quakes), 100), ])

bscols(widths = c(12, 6, 6), filter_slider("stations", "Stations", sd, ~stations), leaflet(sd, width = "100%", 
    height = 400) %>% addTiles() %>% addCircleMarkers(lng = sd$data()[, "long"], lat = sd$data()[, "lat"], 
    stroke = F, fill = T, color = "red", fillOpacity = 0.5, radius = ~mag + 2, label = ~paste0("Depth: ", 
        as.character(depth))), d3scatter(sd, width = "100%", height = 400, ~mag, ~depth, color = ~stations))

Read in data

Read in csv data sources directly from web

# link to raw csv link on e.g. github
url <- "https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv"
flights <- read.csv(url)

Rmarkdown


Split page into three columns (displays best in browser).
R code is in Rmd file.

# r plot code
require(ggplot2)
ggplot(mtcars, aes(x = mpg)) + geom_histogram(fill = "skyblue", alpha = 0.5) + theme_classic()



Praise the lord, I was born to travel
Feeling like Slash in front of the chapel
I’m leaned back with the Les Paul
Shit I smoke is like cholesterol
Spilled dressin’ on the vest at the festival
The best of all, had a midget Puerto Rican at my beckon call



Pump the bass in the trunk
It rattled like a baby hand
Except this toy cost 80 grand
And I’m crazy tan, from all the places that I’ve been
Just from writing words with a pen


plotly

HTML widget with plotly and crosstalk

require(plotly)
require(tidyr)
require(crosstalk)

m <- gather(mpg, variable, value, -c(year, cyl))
msd <- highlight_key(m, ~variable)
gg <- ggplot(m, aes(factor(year), value)) + geom_jitter(alpha = 0.3) + labs(x = "Year") + theme_classic()

bscols(widths = c(11, rep(5, 2)), filter_select("id", "Select a variable", msd, ~variable, multiple = F), 
    ggplotly(gg, dynamicTicks = "y") %>% layout(margin = list(l = 30)), plot_ly(msd, x = ~jitter(cyl), 
        y = ~value, alpha = ~cyl, linetype = NULL, mode = "markers", hoverinfo = "text", text = ~paste0("Cyl: ", 
            round(cyl), "\n", variable, ": ", value, "\nYear: ", year)) %>% add_markers(alpha = 0.3) %>% 
        layout(xaxis = list(showgrid = F, title = "Cylinder"), yaxis = list(showgrid = F)))